package com.farm.shop.controller;

import com.farm.device.pojo.Device;
import com.farm.entity.PageResult;
import com.farm.entity.Result;
import com.farm.entity.StatusCode;
import com.farm.shop.pojo.Shop;
import com.farm.shop.service.FarmShopService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.farm.constant.MessageConstant;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/shop")
public class FarmShopController {
    @Autowired
    private FarmShopService farmShopService;


    /**
     * 查询所有订单
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){
        List<Shop> shops = null;
        try {
            shops = farmShopService.selectAll();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,MessageConstant.SELECT_FARM_SHOP_SUCCESS );
        }
        return new Result(true,StatusCode.OK,MessageConstant.SELECT_FARM_SHOP_FAIL);
    }

    /**
     * 分页搜索
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/findPage/{page}/{size}")
    public Result findPage(@PathVariable Integer page, @PathVariable Integer size){
        try {
            PageResult page1 = farmShopService.findPage(page, size);
            return new Result(true,StatusCode.OK,MessageConstant.SELECT_FARM_SHOP_SUCCESS,page1);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,MessageConstant.SELECT_FARM_SHOP_FAIL);
        }
    }



    /**
     * 根据id删除订单
     * @param orderId
     * @return
     */

    @DeleteMapping("/deleteById/{orderId}")
    public Result deleteById(@PathVariable String orderId){
        try {
            farmShopService.deleteById(orderId);
            return new Result(true,StatusCode.OK,MessageConstant.DELETE_FARM_SHOP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,MessageConstant.DELETE_FARM_SHOP_FAIL);
        }
    }

    /**
     * 模糊查询
     * @return
     */
    @GetMapping("/dimfindPage")
    public Result findPage(  @RequestParam(value = "page",defaultValue = "0",required = false) Integer page, @RequestParam(value = "size",defaultValue = "10",required = false) Integer size,@RequestParam Map<String,Object> map){
        PageResult pageResult= null;
        try {
            Page<Device> pageList = farmShopService.findPage(map, page, size);
            pageResult = new PageResult(pageList.getTotal(),pageList.getResult());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,StatusCode.OK,MessageConstant.SELECT_FARM_SHOP_FAIL);
        }
        return new Result(true,StatusCode.OK,MessageConstant.SELECT_FARM_SHOP_SUCCESS,pageResult);
    }

    @PostMapping("/addShop")
    public Result addShop(@RequestParam("deviceId") String deviceId,@RequestParam("num") Integer num,@RequestParam("custId") String custId,@RequestParam("orderId") String orderId){
        try {
            farmShopService.addShop(deviceId, num, custId,orderId);
            return new Result(true,StatusCode.OK,MessageConstant.ADD_FARM_SHOP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,MessageConstant.ADD_FARM_SHOP_FAIL);
        }
    }
}
